package com.sino.operation.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * TOD
 *
 * @author Administrator
 * date 2021/8/12 0012 23:42
 */
@Component
@Slf4j
public class ServletInfoUtil {

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 获取当前连接token的柜员工号
     *
     * @return
     */
    public String getServletInfo() {
        ServletRequestAttributes sr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sr.getRequest().getHeader("token");
        log.info("获取用户信息", token);
        if (token != null) {
            String tellerCode = jwtUtil.parseJwt(token).containsKey("tellerCode") ? jwtUtil.parseJwt(token).get("tellerCode").toString() : null;
            return tellerCode;
        }
        return null;
    }

    /**
     * 通过柜员号生成token
     *
     * @param tellerCode 柜员工号
     * @return
     */
    public String setServletInfo(String tellerCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("tellerCode", tellerCode);
        return jwtUtil.createJwt(map);
    }
}
